package com.dmx.fw.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.dmx.fw.bean.PageInfo;
import com.dmx.fw.bean.QueryInfoBean;
import com.dmx.fw.component.util.QueryHelper;
import com.dmx.fw.dao.interfaces.JdbcDao;
import com.dmx.fw.resource.SystemProperties;
import com.dmx.fw.service.interfaces.QueryService;

/** 
 * @Description: 通用查询组件Service接口的实现类
 * @author: siyipin
 * @date: 2006
 * @version V1.0 
 * 
 */
public class QueryServiceImpl extends BaseServiceImpl implements QueryService{
	
	public List queryBySql(String sql) {
		// TODO Auto-generated method stub
		return dao.queryData(sql);
	}

	private JdbcDao dao;

    /**
     * Set the Dao for communication with the data layer.
     * @param dao
     */
    public void setJdbcDao(JdbcDao dao) {
        this.dao = dao;
    }
	
	private final static Logger log = Logger.getLogger(QueryServiceImpl.class);
	
	public PageInfo queryData(QueryInfoBean queryInfoBean,Map paramsMap,PageInfo pageInfo){
		return queryData(queryInfoBean,paramsMap,pageInfo,"LIST");
	}

	public PageInfo queryData(QueryInfoBean queryInfoBean,Map paramsMap,PageInfo pageInfo,String resultType){
		pageInfo.setSearchResult(new ArrayList());
		
		if (queryInfoBean==null){
			return pageInfo;
		}
		//原始的没有经过条件替换的sql
		String originalSQL = queryInfoBean.getQueryScript();
		if (originalSQL == null || "".equals(originalSQL)) {
			return pageInfo;
		}

		int iCurrentPage = pageInfo.getCurrentPageNo();
		int iRowsPerPage = pageInfo.getPageSize();
		List dataList = null;	
		try {
							
			pageInfo.setTotalPage(0);
			
			//把原始的SQL中的查询条件占位符替换为用户输入的条件值，替换后的SQL包含orderby的占位符
			String querySQL = QueryHelper.createQuerySQL(originalSQL, paramsMap, queryInfoBean.getConditionsMap());
			
			//获得结果总数，输入的SQL是把orderby占位符剔除掉的sql
			int iTotalRows = dao.getTotalCount(QueryHelper.evictOrderByInfo(querySQL));
			if (pageInfo.getPageSize()==0){
				pageInfo.setPageSize(Integer.MAX_VALUE); 
				pageInfo.setTotalResult(iTotalRows);
				pageInfo.setPageSize(0);
			}else{
				pageInfo.setTotalResult(iTotalRows);
			}
			
			
			if (pageInfo.getPageSize()<=0){
				if (iTotalRows>0)
					pageInfo.setTotalPage(1);
				else 
					pageInfo.setTotalPage(0);
			}else{
		        if(iTotalRows % pageInfo.getPageSize() == 0)
		        	pageInfo.setTotalPage(iTotalRows / pageInfo.getPageSize());
		        else
		        	pageInfo.setTotalPage(iTotalRows / pageInfo.getPageSize()+1);
			}
			
			if (iCurrentPage>pageInfo.getTotalPage()){
				iCurrentPage = pageInfo.getTotalPage();
			}
			
			if (pageInfo.getTotalPage()<=0 && iCurrentPage<=0){
				iCurrentPage = 1;
			}
			pageInfo.setCurrentPageNo(iCurrentPage);
			
			if (iRowsPerPage <= 0) {
				querySQL = QueryHelper.replaceOrderByInfo(querySQL, queryInfoBean.getOrderByScript());
			} else {
				if(SystemProperties.getInstance().getCurrentDataBase().equals("mysql")){
					querySQL = QueryHelper.parsePageMysqlSQL(QueryHelper.replaceOrderByInfo(querySQL, queryInfoBean.getOrderByScript()), iRowsPerPage, iTotalRows, iCurrentPage);
				}else{
					querySQL = QueryHelper.parsePageOracleSQL(QueryHelper.replaceOrderByInfo(querySQL, queryInfoBean.getOrderByScript()), iRowsPerPage, iTotalRows, iCurrentPage);
				}
			}
			log.info(querySQL);
			dataList = dao.queryData(querySQL,resultType);
		} finally {
			if (dataList == null) {
				dataList = new ArrayList();
			}
			pageInfo.setSearchResult(dataList);
		}
		return pageInfo;
	}	

}
